<template>
	<view>
		<web-view ref="webview" src="/hybrid/html/draw/public/index.html"></web-view>
		<!-- <iframe :id="id" class="receiver" src="/hybrid/html/quill-editor.html" :style="webStyle" ></iframe> -->
		<auto-toast ref="autoToast"></auto-toast>
	</view>
</template>

<script>
    // 宿主页面脚本
    import {Resource_url,Http_url, pwd} from '@/config/config.js'
    import {
        getCJList,
        lott_chou,
        vo_pic_js_cj,
        lott_user_list
    } from '@/api/useryk.js'
    
    export default {
        onLoad(params) {
            if(params.id){
                this.id = params.id
            }else {
                uni.redirectTo({
                    url: '/pages/index/index'
                })
            }
        },
        onReady() {
            this.init()
        },
        methods: {
            init(){
                // #ifdef H5
                let _this = this
                this.receiver = this.$refs.webview.iframe.contentWindow;
                this.receiver.onUniH5Event = function(action, data){
                    _this._onMessage(action, data)
                }
                // #endif
            },
            _onMessage(action, data) {
                switch (action) {
                    // web-view 初始化完毕
                    case 'onJSBridgeReady':
                        // console.log('初始化完毕')
                        // 获取奖项列表
                        getCJList().then(res => {
                            const list = res.data.map(item => {
                                return {
                                    ...item,
                                    files: Resource_url + item.files
                                }
                            })
                            // 通知脚本页面初始化
                            this.H5PostMessage('loadHtml', {
                                baseUrl: Http_url,
                                imgPath: Resource_url,
                                userToken: this.$store.state.app.userToken,
                                pwd,
                                list
                            })
                        }).catch(err => {
                            this.$util.Tips({
                            	title: err
                            });
                        })
                        // 获取中奖名单
                        lott_user_list().then(res => {
                            const list = res.data
                            this.H5PostMessage('onWinningList', {
                                list
                            })
                        })
                        break
                    // 获取抽中选项
                    case 'getDrawIndex':
                        if(this.chouLoaidng) return 
                        this.chouLoaidng = true
                            lott_chou({
                                id: this.id
                            }).then(res => {
                                
                                // 兑换
                                vo_pic_js_cj({
                                    id: this.id,
                                    lo_id: res.id
                                }).catch(err => {
                                    this.$util.Tips({
                                        title: err
                                    });
                                })
                                
                                // 通知页面进行抽奖动画
                                this.H5PostMessage('onDrawIndex', {
                                    id: res.id,
                                })
                                
                                // setTimeout(() => this.chouLoaidng = null, 1000)
                            }).catch(err => {
                                // setTimeout(() => this.chouLoaidng = null, 1000)
								if(err == '请勿重复参加活动') err='抽奖次数已用完，请勿重复参与'
								this.$refs['autoToast'].open({
								    content: err
								})
                                // this.$util.Tips({
                                // 	title: err
                                // });
                            })
                        break
                    // 窗口点击事件
                    case 'onClick':
                        this.$emit('tap')
                        break  
                }
            },
            // H5发送事件数据
            H5PostMessage(action, params){
                this.receiver.postMessage({action, params})
            }
        }
    }
</script>

<style>
</style>